﻿using System;
using System.Data.SqlClient;
using FlightService;
using System.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FlightServiceTestProject
{
    [TestClass]
    public class UnitTest1
    {
        string filePath;

        string cnnString
        {
            get { return ConfigurationManager.ConnectionStrings["FlightsConnectionString"].ToString(); }
        }

        private SqlConnection cnn;

        public UnitTest1()
        {
            filePath = AppDomain.CurrentDomain.BaseDirectory + @"/Flights.xls";
            cnn = new SqlConnection(cnnString);
            cnn.Open();
        }
        /// <summary>
        /// A test for Default option, no return, few flights.
        /// </summary>
        [TestMethod]
        public void DefaultOptionTest1()
        {
            string xml =
                "<Flights option=\"Default\" return=\"false\" adults=\"2\" children=\"1\"><Flight from=\"Warsaw\" date=\"01-08-2013\"/><Flight from=\"Amsterdam\" date=\"05-08-2013\"/></Flights>";

            string expected =
               "<Flights><Flight dateFrom=\"2013-08-01 10:00:00\" dateTo=\"2013-08-01 12:00:00\" cityFrom=\"Warsaw\" cityTo=\"Amsterdam\" airline=\"KLM\" price=\"80\"/></Flights>";


            Request request = XmlParser.GetRequestOptionsFromXml(xml);
            request.Flights = XmlParser.XmlToFlights(xml);
            string response = (new DefaultAlgorithm()).Execute(request, cnn);

            Assert.AreEqual(expected, response);
        }

        /// <summary>
        /// A test for Default option, with return, many flights.
        /// </summary>
        [TestMethod]
        public void DefaultOptionTest2()
        {
            string xml = "<Flights option=\"Default\" return=\"true\" adults=\"2\" children=\"3\"><Flight from=\"Warsaw\" date=\"01-08-2013\"/><Flight from=\"Amsterdam\" date=\"05-08-2013\"/><Flight from=\"London\" date=\"07-08-2013\"/><Flight from=\"Madrid\" date=\"08-08-2013\"/><Flight from=\"Berlin\" date=\"10-08-2013\"/><Flight from=\"Paris\" date=\"13-08-2013\"/></Flights>";


            string expected =
                "<Flights><Flight dateFrom=\"2013-08-01 10:00:00\" dateTo=\"2013-08-01 12:00:00\" cityFrom=\"Warsaw\" cityTo=\"Amsterdam\" airline=\"KLM\" price=\"120\"/><Flight dateFrom=\"2013-08-05 10:00:00\" dateTo=\"2013-08-05 12:00:00\" cityFrom=\"Amsterdam\" cityTo=\"London\" airline=\"KLM\" price=\"247\"/><Flight dateFrom=\"2013-08-07 15:00:00\" dateTo=\"2013-08-07 19:00:00\" cityFrom=\"London\" cityTo=\"Madrid\" airline=\"Ryanair\" price=\"97\"/><Flight dateFrom=\"2013-08-08 10:05:00\" dateTo=\"2013-08-08 12:30:00\" cityFrom=\"Madrid\" cityTo=\"Berlin\" airline=\"Ryanair\" price=\"217\"/><Flight dateFrom=\"2013-08-10 21:00:00\" dateTo=\"2013-08-10 22:10:00\" cityFrom=\"Berlin\" cityTo=\"Paris\" airline=\"Ryanair\" price=\"205\"/><Flight dateFrom=\"2013-08-13 19:25:00\" dateTo=\"2013-08-13 21:30:00\" cityFrom=\"Paris\" cityTo=\"Warsaw\" airline=\"Ryanair\" price=\"155\"/></Flights>";

            Request request = XmlParser.GetRequestOptionsFromXml(xml);
            request.Flights = XmlParser.XmlToFlights(xml);
            string response = (new DefaultAlgorithm()).Execute(request, cnn);

            Assert.AreEqual(expected, response);
        }

        [TestMethod]
        public void PriceOptionTest1()
        {
            string xml =
                "<Flights option=\"Price\" return=\"false\" adults=\"2\" children=\"1\"><Flight from=\"Warsaw\" date=\"01-08-2013\"/><Flight from=\"Amsterdam\" date=\"05-08-2013\"/></Flights>";
            string expected =
                "<Flights><Flight dateFrom=\"2013-08-01 10:00:00\" dateTo=\"2013-08-01 12:00:00\" cityFrom=\"Warsaw\" cityTo=\"Amsterdam\" airline=\"KLM\" price=\"80\"/></Flights>";

            Request request = XmlParser.GetRequestOptionsFromXml(xml);
            request.Flights = XmlParser.XmlToFlights(xml);
            string response = (new PriceAlgorithm()).Execute(request, cnn);

            Assert.AreEqual(expected, response);
        }

        [TestMethod]
        public void PriceOptionTest2()
        {
            string xml = "<Flights option=\"Price\" return=\"true\" adults=\"2\" children=\"3\"><Flight from=\"Warsaw\" date=\"01-08-2013\"/><Flight from=\"Amsterdam\" date=\"05-08-2013\"/><Flight from=\"London\" date=\"07-08-2013\"/><Flight from=\"Madrid\" date=\"08-08-2013\"/><Flight from=\"Berlin\" date=\"10-08-2013\"/><Flight from=\"Paris\" date=\"13-08-2013\"/></Flights>";


            string expected =
                "<Flights><Flight dateFrom=\"2013-08-01 10:00:00\" dateTo=\"2013-08-01 12:00:00\" cityFrom=\"Warsaw\" cityTo=\"Amsterdam\" airline=\"KLM\" price=\"120\"/><Flight dateFrom=\"2013-08-05 11:15:00\" dateTo=\"2013-08-05 11:50:00\" cityFrom=\"Amsterdam\" cityTo=\"Berlin\" airline=\"KLM\" price=\"82\"/><Flight dateFrom=\"2013-08-07 21:00:00\" dateTo=\"2013-08-07 22:10:00\" cityFrom=\"Berlin\" cityTo=\"Paris\" airline=\"Ryanair\" price=\"205\"/><Flight dateFrom=\"2013-08-08 15:00:00\" dateTo=\"2013-08-08 16:30:00\" cityFrom=\"Paris\" cityTo=\"London\" airline=\"Ryanair\" price=\"210\"/><Flight dateFrom=\"2013-08-10 15:00:00\" dateTo=\"2013-08-10 19:00:00\" cityFrom=\"London\" cityTo=\"Madrid\" airline=\"Ryanair\" price=\"73.5\"/><Flight dateFrom=\"2013-08-13 17:00:00\" dateTo=\"2013-08-13 20:00:00\" cityFrom=\"Madrid\" cityTo=\"Warsaw\" airline=\"LOT\" price=\"54.5\"/></Flights>";


            Request request = XmlParser.GetRequestOptionsFromXml(xml);
            request.Flights = XmlParser.XmlToFlights(xml);
            string response = (new PriceAlgorithm()).Execute(request, cnn);

            Assert.AreEqual(expected, response);

        }

        [TestMethod]
        public void DistanceOptionTest1()
        {
            string xml =
                "<Flights option=\"Distance\" return=\"false\" adults=\"2\" children=\"1\"><Flight from=\"Warsaw\" date=\"01-08-2013\"/><Flight from=\"Amsterdam\" date=\"05-08-2013\"/></Flights>";

            string expected =
                "<Flights><Flight dateFrom=\"2013-08-01 10:00:00\" dateTo=\"2013-08-01 12:00:00\" cityFrom=\"Warsaw\" cityTo=\"Amsterdam\" airline=\"KLM\" price=\"80\"/></Flights>";

            Request request = XmlParser.GetRequestOptionsFromXml(xml);
            request.Flights = XmlParser.XmlToFlights(xml);
            string response = (new DistanceAlgorithm()).Execute(request, cnn);

            Assert.AreEqual(expected, response);
        }

        [TestMethod]
        public void DistanceOptionTest2()
        {
            string xml = "<Flights option=\"Distance\" return=\"true\" adults=\"2\" children=\"3\"><Flight from=\"Warsaw\" date=\"01-08-2013\"/><Flight from=\"Amsterdam\" date=\"05-08-2013\"/><Flight from=\"London\" date=\"07-08-2013\"/><Flight from=\"Madrid\" date=\"08-08-2013\"/><Flight from=\"Berlin\" date=\"10-08-2013\"/><Flight from=\"Paris\" date=\"13-08-2013\"/></Flights>";


            string expected =
                        "<Flights><Flight dateFrom=\"2013-08-01 10:00:00\" dateTo=\"2013-08-01 12:00:00\" cityFrom=\"Warsaw\" cityTo=\"London\" airline=\"LOT\" price=\"235\"/><Flight dateFrom=\"2013-08-05 15:00:00\" dateTo=\"2013-08-05 16:00:00\" cityFrom=\"London\" cityTo=\"Amsterdam\" airline=\"Ryanair\" price=\"140\"/><Flight dateFrom=\"2013-08-07 15:00:00\" dateTo=\"2013-08-07 15:30:00\" cityFrom=\"Amsterdam\" cityTo=\"Paris\" airline=\"Ryanair\" price=\"145\"/><Flight dateFrom=\"2013-08-08 15:00:00\" dateTo=\"2013-08-08 16:30:00\" cityFrom=\"Paris\" cityTo=\"Madrid\" airline=\"Ryanair\" price=\"210\"/><Flight dateFrom=\"2013-08-10 10:05:00\" dateTo=\"2013-08-10 12:30:00\" cityFrom=\"Madrid\" cityTo=\"Berlin\" airline=\"Ryanair\" price=\"231\"/><Flight dateFrom=\"2013-08-13 09:05:00\" dateTo=\"2013-08-13 10:00:00\" cityFrom=\"Berlin\" cityTo=\"Warsaw\" airline=\"Lufthansa\" price=\"155\"/></Flights>";

            Request request = XmlParser.GetRequestOptionsFromXml(xml);
            request.Flights = XmlParser.XmlToFlights(xml);
            string response = (new DistanceAlgorithm()).Execute(request, cnn);

            Assert.AreEqual(expected, response);
        }

        /// <summary>
        /// A test for "No flights found" when a date is not in the database.
        /// </summary>
        [TestMethod]
        public void NoFlightsTest1()
        {
            string xml =
                        "<Flights option=\"Default\" return=\"true\" adults=\"2\" children=\"1\"><Flight from=\"Warsaw\" date=\"01-02-2013\"/><Flight from=\"Amsterdam\" date=\"17-02-2013\"/></Flights>";

            string expected =
                    "No flights found";

            Request request = XmlParser.GetRequestOptionsFromXml(xml);
            request.Flights = XmlParser.XmlToFlights(xml);
            string response = (new DefaultAlgorithm()).Execute(request, filePath);

            Assert.AreEqual(expected, response);
        }

        /// <summary>
        /// A test for "No flights found" when a city is not in the database.
        /// </summary>
        [TestMethod]
        public void NoFlightsTest2()
        {
            string xml =
                    "<Flights option=\"Default\" return=\"true\" adults=\"2\" children=\"1\"><Flight from=\"Warsaw\" date=\"01-08-2013\"/><Flight from=\"Utrecht\" date=\"13-08-2013\"/></Flights>";

            string expected =
                    "No flights found";

            Request request = XmlParser.GetRequestOptionsFromXml(xml);
            request.Flights = XmlParser.XmlToFlights(xml);
            string response = (new DefaultAlgorithm()).Execute(request, filePath);

            Assert.AreEqual(expected, response);
        }

        /// <summary>
        /// A test for "No flights found" when the number of adults is too big.
        /// </summary>
        [TestMethod]
        public void NoFlightsTest3()
        {
            string xml =
                "<Flights option=\"Default\" return=\"true\" adults=\"9\" children=\"1\"><Flight from=\"Warsaw\" date=\"01-08-2013\"/><Flight from=\"London\" date=\"13-08-2013\"/></Flights>";

            string expected =
                "No flights found";

            Request request = XmlParser.GetRequestOptionsFromXml(xml);
            request.Flights = XmlParser.XmlToFlights(xml);
            string response = (new DefaultAlgorithm()).Execute(request, filePath);

            Assert.AreEqual(expected, response);
        }

        /// <summary>
        /// A test for "No flights found" when the number of children is too big.
        /// </summary>
        [TestMethod]
        public void NoFlightsTest4()
        {
            string xml =
                "<Flights option=\"Price\" return=\"true\" adults=\"3\" children=\"5\"><Flight from=\"Warsaw\" date=\"01-08-2013\"/><Flight from=\"London\" date=\"13-08-2013\"/></Flights>";

            string expected =
                "No flights found";

            Request request = XmlParser.GetRequestOptionsFromXml(xml);
            request.Flights = XmlParser.XmlToFlights(xml);
            string response = (new PriceAlgorithm()).Execute(request, filePath);

            Assert.AreEqual(expected, response);
        }
    }
}
